package com.game;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

public class GameView extends JFrame {

	private static GameView gameView=new GameView();
	private static BorderLayout layout=new BorderLayout();

	/**
	 * 组装 游戏各部分组件
	 */
	public static void main(String[] args) {
		
		try {
			//窗口标题
			gameView.setTitle("拼图游戏");
			gameView.setDefaultCloseOperation(EXIT_ON_CLOSE);//关闭窗口时退出程序
			gameView.setLocation(300, 200);
			gameView.setLayout(layout);
			gameView.setResizable(false);

			Image icon = ImageIO.read(ClassLoader.getSystemResourceAsStream("Photo/logo.png"));
			gameView.setIconImage(icon);
			
			//1.添加游戏标题
			JLabel l=new JLabel("快乐游戏",SwingConstants.CENTER);
			l.setPreferredSize(new Dimension(600,50));
			gameView.add(l,layout.NORTH);
			
			//2.添加右侧参考原图
			JPanel panel = new JPanel(){
				 @Override
				public void paint(Graphics g) {
					 Image buffer=SplitImage.reSizeImage();
					 g.drawImage(buffer,0,0,null,this); //最后绘制到JPanel上
				}
			};
			panel.setPreferredSize(new Dimension(300, 400));//关键代码,设置JPanel的大
			gameView.add(panel,layout.EAST);
			
			//3.添加拼图板
			gameView.add(GamePanel.init(), layout.CENTER);//拼图板
			
			//4.添加底部功能按钮
			JButton bt1=new JButton("背景音乐开");
 			bt1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					GameMusic.playBgMusic();
				}
			});
 			JButton bt2=new JButton("背景音乐关");
			bt2.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
					GameMusic.stopBgMusic();
				}
			});
			JButton bt3=new JButton("选择图片");
			bt3.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					// TODO Auto-generated method stub
					JFileChooser jfc=new JFileChooser();//文件选择控件
					jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);//只能打开文件
					jfc.setFileFilter(new FileNameExtensionFilter(".jpg","jpg"));
					jfc.setFileFilter(new FileNameExtensionFilter(".gif","gif"));
					jfc.setFileFilter(new FileNameExtensionFilter(".png","png"));

					int flag=jfc.showOpenDialog(null);//显示文件选择控件
					File file=jfc.getSelectedFile();  //获取选择文件
                    if(file!=null && flag==JFileChooser.APPROVE_OPTION){
                    	try {
                    		//删除原来拼图板
                    		JPanel panel=(JPanel) layout.getLayoutComponent(layout.CENTER);
							gameView.remove(panel);
				            
							//添加新拼图板
				            gameView.add(GamePanel.init(file.getPath()),layout.CENTER);
				            gameView.validate();//重构内容面板  
				            gameView.repaint();	// 重绘内容面板
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
                    } 
				}
			});
			
			//组装底部按钮
			JPanel btnP=new JPanel();
			btnP.add(bt1);
			btnP.add(bt2);
			btnP.add(bt3);
							   
			gameView.add(btnP,layout.SOUTH);
			
			gameView.pack();//自适应大小
			gameView.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

















